package de.hb.bv1.example;

import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;

/**
 * Example of how to separate logic and gui
 * All Plugins have to implement the PlugInFilter
 * 
 * @author jworch
 * @version 2011-11-09
 *
 */
public class PluginExample implements PlugInFilter {

  /**
   * Used to identify which filter to run
   * @author jworch
   * @see plugins.config
   */
  private enum PluginFunctions {
    NONE, INVERTER, INVERTER_DIAGONAL,
  };
  private PluginFunctions func = PluginFunctions.NONE;

  /**
   * Method which is called when the filter is loaded
   * 
   * @param arg the argument given in plugins.config
   * @param imp the currently active image
   */
  @Override
  public int setup(String arg, ImagePlus imp) {
    for (PluginFunctions inv : PluginFunctions.values()) {
      if (inv.name().equals(arg)) {
        func = inv;
        break;
      }
    }
    //we can handle 8-bit grayscale images
    return DOES_8G;
  }

  /** 
   * Called once to process the image
   */
  @Override
  public void run(ImageProcessor ip) {
    long startTime = System.nanoTime();
    switch (func) {
    case INVERTER:
      // (!)
      Logic.invert(ip);
      break;
    case INVERTER_DIAGONAL:
      // (!)
      Logic.invertDiagonal(ip);
      break;
    }
    System.out.print(String.format("%s processing time: %.4fs\n", func.name(),
        (System.nanoTime() - startTime) * 1e-9));
  }

}
